package zhihui.mobile.ui.shop.list;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.zgm.utils.ui.UiSearchHeader;

import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.shop.ShopListPresenter;
import zhihui.mobile.ui.BaseActivity;
import zhihui.mobile.ui.utils.ShopItemUtils;

/**
 * Created by 张公铭 on 2017/8/1 0001.
 */

public class ShopListActivity extends BaseActivity implements IShopListContract.IShopListView
{
    private ShopListPresenter listPresenter = new ShopListPresenter();
    private UiSearchHeader mHeader;
    private TextView tv_synthesize;
    private TextView tv_sales;
    private RelativeLayout rl_price;
    private TextView tv_price;
    private ImageView iv_price;
    private LinearLayout ll_noraml;
    private PullToRefreshListView lv_content;

    private ShopListAdapter adapter;
    private String descType = "";
    private String desc = "desc";

    private int dataType = 0;   //0是分类，1是搜索

    @Override
    public void initView()
    {
        mHeader = (UiSearchHeader) findViewById(R.id.header_shop_list);
        mHeader.setEditTextBackgroud();
        mHeader.setLeftButtonIcon(R.drawable.shop_back);
        mHeader.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goBack(ShopListActivity.class);
            }
        });

        mHeader.hindRightButtonTwo();
        mHeader.hindRightButtonTwoNum();
        mHeader.setRightOneButtonIcon(R.drawable.black_cart);
        mHeader.setRightButtonOneOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                listPresenter.goShopCart(ShopListActivity.this, ShopListActivity.this);
            }
        });
        mHeader.setEdittextHind("搜索你要的商品");
        mHeader.editeRequestFocus();
        mHeader.setListener(new UiSearchHeader.IOnSearchListener()
        {
            @Override
            public void searchText(String text)
            {
                listPresenter.search(text);
                tv_synthesize.setTextColor(getResources().getColor(R.color.shop_text_select));
                iv_price.setImageResource(R.drawable.price_order);
                iv_price.setTag("normal");
                dataType = 1;
            }
        });

        tv_synthesize = (TextView) findViewById(R.id.tv_shop_list_synthesize);
        tv_synthesize.setTextColor(getResources().getColor(R.color.shop_text_select));
        tv_synthesize.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                updateDescType(v);
            }
        });

        tv_sales = (TextView) findViewById(R.id.tv_shop_list_sales);
        tv_sales.setTextColor(getResources().getColor(R.color.shop_text_normal));
        tv_sales.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                updateDescType(v);
            }
        });

        rl_price = (RelativeLayout) findViewById(R.id.rl_shop_list_price);
        rl_price.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                updateDescType(v);
            }
        });

        tv_price = (TextView) findViewById(R.id.tv_shop_list_price);
        tv_price.setTextColor(getResources().getColor(R.color.shop_text_normal));
        iv_price = (ImageView) findViewById(R.id.iv_shop_list_price);
        iv_price.setImageResource(R.drawable.price_order);
        iv_price.setTag("normal");

        lv_content = (PullToRefreshListView) findViewById(R.id.lv_shop_list_shops);
        lv_content.setMode(PullToRefreshBase.Mode.BOTH);
        adapter = new ShopListAdapter(this);
        adapter.setListener(new ShopListAdapter.IItemOnClickListener()
        {
            @Override
            public void goShopDetails(int shopId, int type)
            {
                listPresenter.goShopDetails(ShopListActivity.this, shopId, type, ShopListActivity.this);
            }
        });
        lv_content.setAdapter(adapter);
        lv_content.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>()
        {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                if (dataType == 0)
                {
                    listPresenter.refresh(descType, desc);
                } else
                {
                    listPresenter.searchRefresh(descType, desc);
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                if (dataType == 0)
                {
                    listPresenter.loadmore(descType, desc);
                } else
                {
                    listPresenter.searchLoadmore(descType, desc);
                }
            }
        });

        ll_noraml = (LinearLayout) findViewById(R.id.ll_shop_list_normal);
        ll_noraml.setVisibility(View.GONE);
        listPresenter.initData();
        if (dataType == 1)
        {
            listPresenter.initSearchText();
        }
    }

    @Override
    protected void parseArgumentsFormIntent(Intent argIntent)
    {
        dataType = argIntent.getIntExtra("dataType", 0);
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.shop_list_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{listPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        listPresenter.init(this, this);
    }

    @Override
    public void initView(List<SpuInfo> list)
    {
        if (list != null && list.size() > 0)
        {
            ll_noraml.setVisibility(View.GONE);
            adapter.setList(list);
            adapter.notifyDataSetChanged();
        } else
        {
            ll_noraml.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void refreshList(List<SpuInfo> list)
    {
        adapter.setList(list);
        adapter.notifyDataSetChanged();
        onRefreshComplete();
    }

    private void updateDescType(View view)
    {
        tv_synthesize.setTextColor(getResources().getColor(R.color.shop_text_normal));
        tv_sales.setTextColor(getResources().getColor(R.color.shop_text_normal));
        tv_price.setTextColor(getResources().getColor(R.color.shop_text_normal));
        iv_price.setImageResource(R.drawable.price_order);

        if (view == tv_synthesize)
        {
            tv_synthesize.setTextColor(getResources().getColor(R.color.shop_text_select));
            if (dataType == 0)
            {
                listPresenter.refresh("", "");
            } else
            {
                listPresenter.searchRefresh("", "");
            }

        } else if (view == tv_sales)
        {
            tv_sales.setTextColor(getResources().getColor(R.color.shop_text_select));
            if (dataType == 0)
            {
                listPresenter.refresh("1", "desc");
            } else
            {
                listPresenter.searchRefresh("1", "desc");
            }
        } else if (view == rl_price)
        {
            tv_price.setTextColor(getResources().getColor(R.color.shop_text_select));
            if (iv_price.getTag().equals("normal") || iv_price.getTag().equals("desc"))
            {
                iv_price.setImageResource(R.drawable.price_order_asc);
                iv_price.setTag("asc");
                if (dataType == 0)
                {
                    listPresenter.refresh("2", "asc");
                } else
                {
                    listPresenter.searchRefresh("2", "asc");
                }
            } else if (iv_price.getTag().equals("asc"))
            {
                iv_price.setTag("desc");
                iv_price.setImageResource(R.drawable.price_order_desc);
                if (dataType == 0)
                {
                    listPresenter.refresh("2", "desc");
                } else
                {
                    listPresenter.searchRefresh("2", "desc");
                }
            }
        }
    }

    @Override
    public void onRefreshComplete()
    {
        adapter.notifyDataSetChanged();
        lv_content.onRefreshComplete();
    }

    @Override
    public void updateSearchText(String text)
    {
        mHeader.setEdittextDeafalutText(text);
        mHeader.editeRequestFocus();
    }

    @Override
    public void updateCartNum(int num)
    {
        mHeader.setRightButtonOneNum(num);
    }

    @Override
    public void addShopList(List<SpuInfo> list)
    {
        if (list == null || list.size() <= 0)
        {
            showMessage("没有更多商品了");
            lv_content.onRefreshComplete();
            return;
        }
        adapter.addList(list);
        adapter.notifyDataSetChanged();
        lv_content.onRefreshComplete();
    }
}
